//
//  LZTopicVoiceView.m
//  LZHundredJie
//
//  Created by apple on 16/4/15.
//  Copyright © 2016年 m14a.cn. All rights reserved.
//

#import "LZTopicVoiceView.h"
#import "LZTopic.h"
#import "LZSeeBigPictureViewController.h"

@interface LZTopicVoiceView ()

@property (weak, nonatomic) IBOutlet UILabel *playcountLabel;
@property (weak, nonatomic) IBOutlet UILabel *voicetimeLabel;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation LZTopicVoiceView

- (void)awakeFromNib
{
    // 取消自动伸缩
    self.autoresizingMask = UIViewAutoresizingNone;
    
    self.imageView.userInteractionEnabled = YES;
    [self.imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(seeBigPicture)]];
}

/**
 *  查看大图
 */
// 这里把查看大图按钮的和用户交互给禁止调了,因为给后面的图片添加了手势,
// 这样点击整个图片区域都可以监听手势了,都会跳转界面
- (void)seeBigPicture
{
    LZSeeBigPictureViewController *seeBigVC = [[LZSeeBigPictureViewController alloc] init];
    // 顺传
    seeBigVC.topic = self.topic;
    // modal出来一个控制器
    [self.window.rootViewController presentViewController:seeBigVC animated:YES completion:nil];
}



- (void)setTopic:(LZTopic *)topic
{
    _topic = topic;
    // 设置图片
//    [self.imageView sd_setImageWithURL:[NSURL URLWithString:topic.image1] placeholderImage:[UIImage imageNamed:@"defaultUserIcon"]];
    /*
    // 1.创建占位图
    UIImage *placeholderImage = nil;
    // 2.去沙盒里面找大图
    UIImage *bigImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:topic.image1];

    if (bigImage) { // 大图
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:topic.image1] placeholderImage:placeholderImage];
//        self.imageView.image = bigImage;
        
    } else { // 没有大图
        
        AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager manager];
        
        if (mgr.reachableViaWiFi) { // WIFI
            [self.imageView sd_setImageWithURL:[NSURL URLWithString:topic.image1] placeholderImage:placeholderImage];
        } else if (mgr.reachableViaWWAN) {  // 手机
            // 去沙盒里面找是否允许访问4G下载大图
            BOOL isAllow = YES;
            if (isAllow) {  // 允许,大图
                [self.imageView sd_setImageWithURL:[NSURL URLWithString:topic.image1] placeholderImage:placeholderImage];
            } else {    // 不允许，小图
                [self.imageView sd_setImageWithURL:[NSURL URLWithString:topic.image0] placeholderImage:placeholderImage];
            }
            
        } else {    // 没有网
        
            // 1.去沙盒里面找小图
            UIImage *smallImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:topic.image0];
            if (smallImage) {   // 有小图
                [self.imageView sd_setImageWithURL:[NSURL URLWithString:topic.image0] placeholderImage:placeholderImage];
            } else {    // 没有小图,显示占位图片
                [self.imageView sd_setImageWithURL:nil placeholderImage:placeholderImage];
            }
        }
        
    }
    */
    // 中间图片
    [self.imageView xmg_setImageWithOriginalImageURL:topic.image1 thumbnailImageURL:topic.image0];
    
    
    // 设置播放次数
    if (topic.playcount >= 10000) {
        self.playcountLabel.text = [NSString stringWithFormat:@"%.1f万播放", topic.playcount / 10000.0];
    } else {
        self.playcountLabel.text = [NSString stringWithFormat:@"%zd播放", topic.playcount];
    }
    // 设置播放时长,%02zd : 占据2位，多余的空位用0来填补
    self.voicetimeLabel.text = [NSString stringWithFormat:@"%02zd:%02zd", topic.voicetime / 60, topic.voicetime % 60];
}



@end
